//
//  OrderAddTaskViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/4/26.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire

class OrderAddTaskViewController: BaseViewController {
    
    var orderId = ""
    var taskId = ""
    var serviceType = ""
    
    @IBOutlet var serviceTypeTF: UITextField!
    
    @IBOutlet var remarksTF: UITextField!
    
    @IBOutlet var serviceBtn: UIButton!
    
    @IBOutlet var sumbitBtn: UIButton!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.backgroundColor = UIColor.white
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        serviceTypeTF.text = serviceType
    }

    @IBAction func sumbitBtnPress(_ sender: UIButton) {
        let date = NSDate()
        let timeInterval = date.timeIntervalSince1970
        let str = ("\(timeInterval)" as NSString).substring(with: NSRange(location: 0, length: 10))
        if (serviceTypeTF.text == "") || (self.remarksTF.text == "") {
            MBProgressHUD.alertShow(in: self.view, message: "请填写完整再提交", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        if (self.orderId == "") {//编辑
            requestEditTask(Id: taskId, service: serviceTypeTF.text!,remarks: remarksTF.text!)
        }else {//添加
            requestAddTask(orderId: orderId, service: serviceTypeTF.text!, remarks: remarksTF.text!, addtime:str)
        }
        
        
        
    }
    
    @IBAction func serviceBtnPress(_ sender: UIButton) {
        
        let taskVC = TaskChooseViewController()
        taskVC.title = "服务类型选择";
        taskVC.delegate = self; self.navigationController?.pushViewController(taskVC, animated: true)
    }
    
    
    //网络请求
    //添加任务分配
    func requestAddTask(orderId:String,service:String,remarks:String,addtime:String) -> Void {
        
        let param =
            [
             "orderid":orderId,
             "content":service,
             "userid":UserModel.shareUser.data?.uid ?? String(),
             "description":remarks,
             "shopid":UserModel.shareUser.data?.storeId ?? String(),
             "addtime":addtime
        ]
        
        Alamofire.request(addTaskUrl, method:.post, parameters:param)
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = DefaultData()
                    basemodel = DefaultData.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        print("添加成功")
                    self.navigationController?.popViewController(animated: true)
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
                
        }
    }
    
    //编辑任务
        func requestEditTask (Id:String,service:String,remarks:String) -> Void {

            let param =
                [
                    "id":Id,
                    "content":service,
                    "description":remarks,
                ]
            
            Alamofire.request(editTaskUrl, method:.post, parameters:param)
                .responseString { response in
    
                    switch response.result.isSuccess {
                    case true:
    
                        var basemodel = DefaultData()
                        basemodel = DefaultData.deserialize(from: response.value)!
    
                        switch basemodel.status! {
                        case true:
                            print("编辑成功")
    self.navigationController?.popViewController(animated: true)
                        case false:
                             MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                        }
    
                    case false:
                        print(response.result.error!)
                    }
    
            }
        }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension OrderAddTaskViewController: TaskDelegate {
    func postValueToVc(str: String) {
        self.serviceTypeTF.text = str
    }
}

